from datetime import datetime
from .base import BaseModel
from ..extensions.extensions import db

class Role(BaseModel):
    """角色表"""
    __tablename__ = 'roles'
    
    role_name = db.Column(db.String(100), nullable=False, comment='角色名称')
    role_code = db.Column(db.String(50), nullable=False, unique=True, comment='角色代码')
    description = db.Column(db.String(200), comment='角色描述')
    sort_order = db.Column(db.Integer, default=0, comment='排序号')
    status = db.Column(db.Boolean, nullable=False, default=True, comment='状态(1:启用,0:禁用)')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'role_name': self.role_name,
            'role_code': self.role_code,
            'description': self.description,
            'sort_order': self.sort_order,
            'status': self.status,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None
        } 